{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 请简述基于内容的推荐和基于协同过滤的推荐的基本原理，并指出二者的适用场景。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基于内容的推荐  \n",
    "基于内容的推荐是根据用户过去的喜好记录，为其推荐与用户喜欢的item相似的item。  \n",
    "一般步骤为：  \n",
    "1)item表示：抽取item的相关特征来表示此item。    \n",
    "2)用户profile：利用用户过去喜好的item特征数据，学习出用户的喜好特征，即构建用户profile。  \n",
    "3)推荐：比较用户profile和候选item，计算出相关性最大的item组推荐给用户。  \n",
    "适用场景：信息检索系统\n",
    "\n",
    "基于协同过滤的推荐  \n",
    "UserCF是找到与目标用户相似的用户集合，将这些用户喜欢的且目标用户未行为过的item推荐给目标用户。  \n",
    "ItemCF是根据用户的历史行为数据，计算出与用户喜好的item相似的且未行为过的item，生成推荐列表。  \n",
    "适用场景：UserCF电商商品推荐，ItemCF新闻推荐系统  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 请分别给出一个基于用户的协同过滤和基于物品的协同过滤的典型应用场景。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "UserCF和ItemCF都需要维护一个相似度矩阵    \n",
    "对于UserCF,我们希望user-user相似度矩阵相对固定。  \n",
    "在新闻推荐系统中，user的更新速度要远远小于item的更新速度，UserCF较为常用。  \n",
    "对于ItemCF,我们希望item-item相似度矩阵相对固定。  \n",
    "在电商推荐系统中，item的数目相对固定，并且只需要维护用户行为过的item与其他item的相似度矩阵，可以预先计算好相似度，在线预测速度快，因此ItemCF较为常用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 将推荐电影数目改成20个，运行课程给的代码，比较三种协同过滤算法的性能，并和推荐数目为10的推荐结果比较。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>presicion1</th>\n",
       "      <th>presicion2</th>\n",
       "      <th>recall1</th>\n",
       "      <th>recall2</th>\n",
       "      <th>coverage1</th>\n",
       "      <th>coverage2</th>\n",
       "      <th>rmse1</th>\n",
       "      <th>rmse2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Item_CF</th>\n",
       "      <td>0.06230</td>\n",
       "      <td>0.06590</td>\n",
       "      <td>0.01430</td>\n",
       "      <td>0.03025</td>\n",
       "      <td>0.49575</td>\n",
       "      <td>0.72606</td>\n",
       "      <td>1.13249</td>\n",
       "      <td>1.13277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User_CF</th>\n",
       "      <td>0.00108</td>\n",
       "      <td>0.00185</td>\n",
       "      <td>0.00025</td>\n",
       "      <td>0.00085</td>\n",
       "      <td>0.10787</td>\n",
       "      <td>0.17454</td>\n",
       "      <td>0.96586</td>\n",
       "      <td>0.96586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SVD_CF</th>\n",
       "      <td>0.07429</td>\n",
       "      <td>0.07930</td>\n",
       "      <td>0.01705</td>\n",
       "      <td>0.03640</td>\n",
       "      <td>0.10060</td>\n",
       "      <td>0.14484</td>\n",
       "      <td>0.92562</td>\n",
       "      <td>0.92518</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         presicion1  presicion2  recall1  recall2  coverage1  coverage2  \\\n",
       "Item_CF     0.06230     0.06590  0.01430  0.03025    0.49575    0.72606   \n",
       "User_CF     0.00108     0.00185  0.00025  0.00085    0.10787    0.17454   \n",
       "SVD_CF      0.07429     0.07930  0.01705  0.03640    0.10060    0.14484   \n",
       "\n",
       "           rmse1    rmse2  \n",
       "Item_CF  1.13249  1.13277  \n",
       "User_CF  0.96586  0.96586  \n",
       "SVD_CF   0.92562  0.92518  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#presicion1和presicion2分别代表推荐数目10和20的结果\n",
    "index = [\"Item_CF\",\"User_CF\",\"SVD_CF\"]\n",
    "columns = [\"presicion1\",\"presicion2\",\"recall1\",\"recall2\",\\\n",
    "           \"coverage1\",\"coverage2\",\"rmse1\",\"rmse2\"]\n",
    "data = [\n",
    "    [0.06230,0.06590,0.01430,0.03025,0.49575,0.72606,1.13249,1.13277],\n",
    "    [0.00108,0.00185,0.00025,0.00085,0.10787,0.17454,0.96586,0.96586],\n",
    "    [0.07429,0.07930,0.01705,0.03640,0.10060,0.14484,0.92562,0.92518]\n",
    "]\n",
    "result = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "指标分析：  \n",
    "SVD_CF准确率和召回率最高，但覆盖率最低  \n",
    "Item_CF覆盖率最高  \n",
    "User_CF准确率和召回率最低  \n",
    "推荐数目20和10相比，recall有很大提高，几乎成倍增加，coverage也有较大提高，但precision和rmse变化不大  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
